home *** CD-ROM | disk | FTP | other *** search
/ Internet Tools (InfoMagic) / Internet Tools.iso / mail / uucp / uubatch.105.Z / uubatch.105 / uucplock.c < prev    next >
C/C++ Source or Header  |  1993-06-07  |  5KB  |  186 lines

  1.  
  2. /* 
  3.  * uucplock.c, uubatch version 1.0.5 :
  4.  *
  5.  * This code is a modified version of the bsd tip uucplock.c source.
  6.  * I got this source from UUNET and modified it a bit, making it 
  7.  * a complete executable program that locks/unlocks an "object".
  8.  * 
  9.  * Below is the original Copyright found in the original file :
  10.  *
  11.  */
  12.  
  13. /*
  14.  * Copyright (c) 1988 The Regents of the University of California.
  15.  * All rights reserved.
  16.  *
  17.  * Redistribution and use in source and binary forms are permitted
  18.  * provided that: (1) source distributions retain this entire copyright
  19.  * notice and comment, and (2) distributions including binaries display
  20.  * the following acknowledgement:  ``This product includes software
  21.  * developed by the University of California, Berkeley and its contributors''
  22.  * in the documentation or other materials provided with the distribution
  23.  * and in all advertising materials mentioning features or use of this
  24.  * software. Neither the name of the University nor the names of its
  25.  * contributors may be used to endorse or promote products derived
  26.  * from this software without specific prior written permission.
  27.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  28.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  29.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  30.  */
  31.  
  32. #include <sys/types.h>
  33. #include <sys/file.h>
  34. #include <sys/dir.h>
  35. #include <errno.h>
  36. #ifndef NOHDB
  37. #include <stdio.h>
  38. #endif
  39.  
  40. #ifdef SCO
  41. #include <limits.h>
  42. #include <fcntl.h>
  43. #include <unistd.h>
  44. #include <string.h>
  45. #define PMAX _POSIX_PATH_MAX
  46. #define SetPosition SEEK_SET
  47. #define index(_a,_b) strchr(_a,_b)
  48. #else
  49. #define PMAX MAXNAMLEN
  50. #define SetPosition L_SET
  51. #endif
  52.  
  53. #include "patchlevel.h"
  54.  
  55. /* 
  56.  * uucp style locking routines
  57.  * return: 0 - success
  58.  *       -1 - failure
  59.  */
  60.  
  61.  
  62. uu_lock(objname)
  63.     char *objname;
  64. {
  65.     extern int errno;
  66.     int fd, pid;
  67.     char spid[256];
  68.     char tbuf[sizeof(_PATH_LOCKDIRNAME) + PMAX];
  69.     off_t lseek();
  70. #ifndef NOHDB
  71.     FILE *ffd;
  72. #endif
  73.  
  74.     (void)sprintf(tbuf, _PATH_LOCKDIRNAME, objname);
  75.     fd = open(tbuf, O_RDWR|O_CREAT|O_EXCL, 0666);
  76.     if (fd < 0) {
  77.         /*
  78.          * file is already locked
  79.          * check to see if the process holding the lock still exists
  80.          */
  81.         fd = open(tbuf, O_RDWR, 0);
  82.         if (fd < 0) {
  83.             perror("lock open");
  84.             return(-1);
  85.         }
  86. #ifdef NOHDB
  87.         /* this is old BSD code, it reads the number in binary */
  88.         /* form. This does not go well with HDB...        */
  89.             if (read(fd, &pid, sizeof(pid)) != sizeof(pid)) {
  90. #else
  91.         /* This is code for HDB UUCP, it puts the pid in as text : */
  92.             if (!(ffd=fdopen(fd,"r+"))) {
  93.                 perror("fdopen : lock open");
  94.                 return(-1);
  95.             }
  96.             if (!fscanf(ffd,"%d",&pid)) {
  97. #endif
  98.                 (void)close(fd);
  99.                 perror("lock read");
  100.                 return(-1);
  101.             }
  102.  
  103.             if (kill(pid, 0) == 0 || errno != ESRCH) {
  104.                 (void)close(fd);    /* process is still running */
  105.                 return(-1);
  106.             }
  107.             /*
  108.              * The process that locked the file isn't running, so
  109.              * we'll lock it ourselves
  110.              */
  111.             if (lseek(fd, 0L, SetPosition) < 0) {
  112.                 (void)close(fd);
  113.                 perror("lock lseek");
  114.                 return(-1);
  115.             }
  116.             /* fall out and finish the locking process */
  117.         }
  118.     
  119.     pid = getppid();    /* Parent process is the shell */
  120. #ifdef NOHDB
  121.     /* this is old BSD lock code, it writes the number in in binary */
  122.     /* form. This does not go well with HDB...            */
  123.     if (write(fd, (char *)&pid, sizeof(pid)) != sizeof(pid)) {
  124.         (void)close(fd);
  125.         (void)unlink(tbuf);
  126.         perror("lock write");
  127.         return(-1);
  128.     }
  129. #else
  130.     /* This is for HDB UUCP : it puts the pid in as text...        */
  131.     sprintf(spid,"%10d\n",pid);
  132.     if (write(fd, spid, strlen(spid)) != strlen(spid)) {
  133.         (void)close(fd);
  134.         (void)unlink(tbuf);
  135.         perror("lock write");
  136.         return(-1);
  137.     }
  138. #endif
  139.     (void)close(fd);
  140.     return(0);
  141. }
  142.  
  143. uu_unlock(objname)
  144.     char *objname;
  145. {
  146.     char tbuf[sizeof(_PATH_LOCKDIRNAME) + PMAX];
  147.  
  148.     (void)sprintf(tbuf, _PATH_LOCKDIRNAME, objname);
  149.     return(unlink(tbuf));
  150. }
  151.  
  152. p_usage(s)
  153. char *s;
  154. {
  155.     fprintf(stderr," FAILED. Wrong usage.\n");
  156.     fprintf(stderr," Usage : %s lock|unlock objname\n");
  157.     fprintf(stderr,"         Will return SUCCES or FAILED string.\n");
  158.     exit(-1);
  159. }
  160.  
  161. main(argc,argv)
  162. int argc;
  163. char **argv;
  164. {
  165.  
  166.     int failed;
  167.     if (argc!=3)
  168.         p_usage(argv[0]);
  169.  
  170.     if (!strcmp(argv[1],"lock"))
  171.     {
  172.         fprintf(stderr,"lock    : %s\n",
  173.             (failed=uu_lock(argv[2])) ? "FAILED" : "SUCCESS" );
  174.         exit(failed ? 1 : 0);
  175.     }
  176.     if (!strcmp(argv[1],"unlock"))
  177.     {
  178.         fprintf(stderr,"unlock  : %s\n",
  179.             (failed=uu_unlock(argv[2])) ? "FAILED" : "SUCCESS" );
  180.         exit(failed ? 1 : 0);
  181.     }
  182.  
  183.     /* If we get here then wrong usage : */
  184.     p_usage(argv[0]);
  185. }
  186.